<?php

namespace App\Listener;

use App\Amqp\Producer\UpdateUserScoreProducer;
use App\Event\HangupVideoEvent;
use App\Event\RechargeEvent;
use App\Event\RegDoneEvent;
use Hyperf\Amqp\Producer;
use Hyperf\Event\Contract\ListenerInterface;
use Hyperf\Utils\ApplicationContext;
use Hyperf\Event\Annotation\Listener;

/**
 * 更改用户分数
 * @Listener
 */
class UpdateUserScoreListener implements ListenerInterface
{
    public function listen(): array
    {
        return [
            //注册事件
            RegDoneEvent::class,
            //充值事件
            RechargeEvent::class,
            //视频挂断事件
            HangupVideoEvent::class,
        ];
    }

    public function process(object $event)
    {
        switch ($event->eventType) {
            case 'hangup_video':
                $userIds = [$event->answerUserId, $event->userId];
                break;
            case 'reg_done':
            case 'recharge':
            default:
                $userIds = [$event->userId];
                break;
        }

        if (empty($userIds)){
            return;
        }

        foreach ($userIds as $one){
            $message = new UpdateUserScoreProducer(['user_id' => $one]);
            $producer = ApplicationContext::getContainer()->get(Producer::class);
            $producer->produce($message);
        }

    }
}